package com.ayou.eggguard.farm.service.impl;

import com.ayou.common.utils.Md5Util;
import com.ayou.eggguard.farm.mapper.ChickenFarmMapper;
import com.ayou.eggguard.farm.mapper.KeeperMapper;
import com.ayou.eggguard.farm.pojo.Keeper;
import com.ayou.eggguard.farm.service.KeeperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class KeeperServiceImpl implements KeeperService {

    @Autowired
    private KeeperMapper keeperMapper;

    @Autowired
    private ChickenFarmMapper chickenFarmMapper;

    @Override
    public Keeper getKeeperByUsername(String username) {
        return keeperMapper.getKeeperByUsername(username);
    }

    @Override
    public void addKeeper(@RequestParam String farmName, @RequestBody Keeper keeper) {
        int farmId = chickenFarmMapper.getFarmIdByFarmName(farmName);
        String md5String = Md5Util.getMD5String(keeper.getPassword());
        keeper.setPassword(md5String);
        keeperMapper.addKeeper(farmId,keeper);

    }

    @Override
    public List<Keeper> getKeepersByFarmName(String farmName) {
        return keeperMapper.getKeepersByFarmName(farmName);
    }

    @Override
    public void deleteKeeperByUsername(String username) {
        keeperMapper.deleteKeeperByUsername(username);
    }

    @Override
    public void resetKeeperPwd(String username) {
        String md5String=Md5Util.getMD5String("admin");
        keeperMapper.resetKeeperPwd(username,md5String);
    }


}
